<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:fn="http://java.sun.com/jsp/jstl/functions"
      xmlns:composite="http://java.sun.com/jsf/composite"
      xmlns:p="http://primefaces.org/ui">

<composite:interface>
    <!--    default-->
    <composite:attribute name="rendered" default="true"/>
    <composite:attribute name="binding" default="#{entityDialog}"/>
    <composite:attribute name="widgetVar"
                         default="#{fn:substringAfter(cc.clientId,':').replace(':','')}_dialog"/>
    <composite:attribute name="header"
                         default="#{cc.attrs.value.id eq null ? msg['framework']['add'] : msg['framework']['edit']}"/>
    <composite:attribute name="draggable" default="true"/>
    <composite:attribute name="resizable" default="true"/>
    <composite:attribute name="modal" default="true"/>
    <composite:attribute name="visible" default="false"/>
    <composite:attribute name="width"/>
    <composite:attribute name="height" default="auto"/>
    <composite:attribute name="minWidth" default="450"/>
    <composite:attribute name="minHeight" default="150"/>
    <composite:attribute name="style"/>
    <composite:attribute name="styleClass" default="dialog-layout"/>
    <composite:attribute name="showEffect" default="fade"/>
    <composite:attribute name="hideEffect" default="fade"/>
    <composite:attribute name="position" default="top"/>
    <composite:attribute name="closable" default="true"/>
    <composite:attribute name="onShow" default=""/>
    <composite:attribute name="onHide" default=""/>
    <composite:attribute name="appendTo" default=""/>
    <composite:attribute name="showHeader" default="true"/>
    <composite:attribute name="footer" default=""/>
    <composite:attribute name="dynamic" default="false"/>
    <composite:attribute name="cache" default="false"/>
    <composite:attribute name="minimizable" default="true"/>
    <composite:attribute name="maximizable" default="true"/>
    <composite:attribute name="closeOnEscape" default="false"/>
    <composite:attribute name="dir" default="ltr"/>
    <composite:attribute name="focus" default=""/>
    <composite:attribute name="fitViewport" default="true"/>
    <composite:attribute name="positionType" default="fixed"/>
    <composite:attribute name="responsive" default="true"/>
    <composite:attribute name="blockScroll" default="true"/>
    <composite:attribute name="my" default="center"/>


    <!-- coustom attrs-->
    <composite:attribute name="tableWidgetVar"/>
    <composite:attribute name="value"/>
    <composite:attribute name="fullScreen" default="false"/>

    <!-- close dialog-->
    <composite:attribute name="closeSupported" default="true"/>
    <composite:attribute name="closeAjaxSupported" default="true"/>
    <composite:attribute name="closeButtonText" default="#{msg['framework']['cancel']}"/>
    <composite:attribute name="closeMethod"
                         method-signature="void close(javax.faces.component.UIComponent)"
                         default="#{controller.close(component)}"/>
    <composite:attribute name="closeUpdate" default="@none"/>
    <composite:attribute name="closeOncomplete" default="PF('#{cc.attrs.widgetVar}').hide();"/>

    <!-- save button-->
    <composite:attribute name="saveSupported" default="true"/>
    <composite:attribute name="saveValidateClient" default="true"/>
    <composite:attribute name="saveDisabled" default="#{cc.attrs.value eq null}"/>
    <composite:attribute name="saveButtonText" default="#{msg['framework']['confirm']}"/>
    <composite:attribute name="saveMethod" method-signature="void save()" default="#{controller.save}"/>
    <composite:attribute name="saveUpdate" default="@none"/>
    <composite:attribute name="saveComplete"/>

    <!-- other attrs -->
    <composite:attribute name="contentStyleClass" default="grid"/>
    <composite:attribute name="showMessages" default="true"/>


    <composite:facet name="button"/>
</composite:interface>

<composite:implementation>

    <script>
        function #{cc.attrs.widgetVar}_handleComplete(xhr, status, args) {
            if (!args.validationFailed) {
                PF('#{cc.attrs.widgetVar}').hide();
            }
        }
    </script>
    <p:hotkey bind="enter" handler="PF('saveButton').jq.click()"/>
    <p:hotkey bind="esc" handler="PF('cancelButton').jq.click()"/>
    <p:dialog id="entityDialog"
              rendered="#{cc.attrs.rendered}"
              binding="#{cc.attrs.binding}"
              widgetVar="#{cc.attrs.widgetVar}"
              header="#{cc.attrs.header}"
              draggable="#{cc.attrs.draggable}"
              resizable="#{cc.attrs.resizable}"
              modal="#{cc.attrs.modal}"
              visible="#{cc.attrs.visible}"
              width="#{cc.attrs.width}"
              height="#{cc.attrs.height}"
              minWidth="#{cc.attrs.minWidth}"
              minHeight="#{cc.attrs.minHeight}"
              style="#{cc.attrs.style};"
              styleClass="#{cc.attrs.styleClass} #{cc.attrs.widgetVar}"
              showEffect="#{cc.attrs.showEffect}"
              hideEffect="#{cc.attrs.hideEffect}"
              position="#{cc.attrs.position}"
              closable="#{cc.attrs.closable}"
              onShow="#{cc.attrs.onShow}; if(#{cc.attrs.fullScreen}) PF('#{cc.attrs.widgetVar}').toggleMaximize();"
              onHide="#{cc.attrs.onHide}"
              appendTo="#{cc.attrs.appendTo}"
              showHeader="#{cc.attrs.showHeader}"
              footer="#{cc.attrs.footer}"
              dynamic="#{cc.attrs.dynamic}"
              cache="#{cc.attrs.cache}"
              minimizable="#{cc.attrs.minimizable}"
              maximizable="#{cc.attrs.maximizable}"
              closeOnEscape="#{cc.attrs.closeOnEscape}"
              dir="#{cc.attrs.dir}"
              focus="#{cc.attrs.focus}"
              fitViewport="#{cc.attrs.fitViewport}"
              positionType="#{cc.attrs.positionType}"
              responsive="#{cc.attrs.responsive}"
              blockScroll="#{cc.attrs.blockScroll}"
              my="#{cc.attrs.my}">
        <composite:insertFacet name="header"/>
        <p:ajax event="close" disabled="#{not(cc.attrs.closeSupported and cc.attrs.closeAjaxSupported)}"
                listener="#{cc.attrs.closeMethod}" update="#{cc.attrs.closeUpdate}" global="false"/>
        <p:messages id="dialogMessages" widgetVar="dialogMessages" styleClass="dialog-messages" for="dialogMessages"
                    showDetail="true" showSummary="true">
            <p:autoUpdate/>
        </p:messages>
        <p:outputPanel id="contentPanel" styleClass="dialog-content-panel">
            <p:outputPanel rendered="#{!cc.attrs.saveDisabled}" styleClass="#{cc.attrs.contentStyleClass}">
                <composite:insertChildren/>
            </p:outputPanel>
        </p:outputPanel>
        <f:facet name="footer">
            <composite:renderFacet name="button"/>
            <p:outputPanel rendered="#{cc.attrs.saveSupported}" layout="inline">
                <p:commandButton id="saveButton" widgetVar="saveButton" styleClass="entity-dialog-save-btn"
                                 icon="pi pi-check"
                                 process="@form" validateClient="#{cc.attrs.saveValidateClient}"
                                 value="#{cc.attrs.saveButtonText}"
                                 action="#{cc.attrs.saveMethod}"
                                 disabled="#{cc.attrs.saveDisabled}"
                                 update=":#{cc.clientId}:contentPanel, #{cc.attrs.saveUpdate}"
                                 oncomplete="#{cc.attrs.widgetVar}_handleComplete(xhr, status, args); #{cc.attrs.saveComplete}"/>
            </p:outputPanel>
            <p:outputPanel rendered="#{cc.attrs.closeSupported}" layout="inline">
                <p:commandButton id="cancelButton" widgetVar="cancelButton"
                                 styleClass="entity-dialog-cancel-btn ui-button-secondary"
                                 icon="pi pi-times" process="@this" immediate="true"
                                 value="#{cc.attrs.closeButtonText}" global="false"
                                 oncomplete="#{cc.attrs.closeOncomplete}"/>
            </p:outputPanel>
        </f:facet>
    </p:dialog>
</composite:implementation>
</html>
